package com.lego.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lego.service.OrdersService;
import com.lego.service.ReviewsService;
import com.lego.service.UserService;
import com.lego.service.impl.OrdersServiceImpl;
import com.lego.service.impl.ReviewsServiceImpl;
import com.lego.service.impl.UserServiceImpl;

@WebServlet("/deleteUserInfo.do")
public class DeleteUserInfoServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserService us = new UserServiceImpl();
		OrdersService ods = new OrdersServiceImpl();
		ReviewsService rs = new ReviewsServiceImpl();
		request.setCharacterEncoding("utf-8");
		int userId = 0;
		try {
			userId = Integer.parseInt(request.getParameter("userId"));
		} catch (Exception e) {
			System.out.println("没有获取到userId");
		}
		String str = "";//删除方式
		//查询是否有过交易记录（订单）
		int totalRows = ods.selectUserRows(null,userId);
		if(totalRows<=0) {//如果没有直接删除账户
			int rowNum = us.deleteUser(userId);
			str = "删除";
		}else {//如果有软删除账户并且删除评论
			//删除评论
			int row1 = rs.delComentByUserId(userId);
			//修改账户状态
			int row2 = us.changeUserStatus(0,userId);
			str = "修改";
			
		}
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().write(str);
	}
}
